<template>
  <div>
    <img
      style="width: 100%; height: 100%"
      src="https://passport.baidu.com/static/passpc-account/img/reg_bg_min.jpg"
    />
    <el-form
      :model="tenantDto.tenant"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="content"
    >
      <el-form-item label="用户名" prop="name">
        <el-input
          type="text"
          v-model="tenantDto.tenant.name"
          autocomplete="off"
          placeholder="请输入用户名"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pwd">
        <el-input
          type="password"
          v-model="tenantDto.tenant.pwd"
          autocomplete="off"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="pwd1">
        <el-input
          type="password"
          v-model="checkPass"
          autocomplete="off"
          placeholder="请再次输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          type="text"
          v-model="tenantDto.tenant.companyName"
          autocomplete="off"
          placeholder="请输入公司名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="公司编码" prop="companyCode">
        <el-input
          type="text"
          v-model="tenantDto.tenant.companyCode"
          autocomplete="off"
          placeholder="请输入公司编码"
        ></el-input>
      </el-form-item>
      <el-form-item label="电话号码" prop="phone">
        <el-input
          type="text"
          v-model="tenantDto.tenant.phone"
          placeholder="请输入手机号码"
        ></el-input>
      </el-form-item>
      <el-form-item label="验证码">
        <el-input
          type="text"
          v-model="tenantDto.code"
          placeholder="请输入验证码"
        ></el-input>
        <el-button
          @click.stop="sendCode"
          size="mini"
          type="primary"
          style="margin-left: 10px"
          v-if="show"
        >
          发送验证码
        </el-button>
        <el-button
          size="mini"
          type="primary"
          style="margin-left: 10px"
          v-if="!show"
          disabled
        >
          {{ count }}秒后重发
        </el-button>
      </el-form-item>
      <el-form-item label="描述">
        <el-input
          type="text"
          v-model="tenantDto.tenant.description"
          placeholder="请输入描述"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >注册</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { tenantRegister, sendCode } from "@/api/register";
export default {
  data() {
    // var validatePass = (rule, value, callback) => {
    //   if (value === "") {
    //     callback(new Error("请再次输入密码"));
    //   } else if (value !== this.tenant.pwd) {
    //     callback(new Error("两次输入密码不一致!"));
    //   } else {
    //     callback();
    //   }
    // };
    return {
      tenantDto: {
        code: "",
        tenant: {},
      },
      count: "",
      show: true,
      checkPass: "",
      rules: {
        pwd: [{ required: true, message: "请输入密码", trigger: "blur" }],
        // pwd1: [{ required: true, message: "请输入密码", trigger: "blur" }],
        // pwd1: [{ validator: validatePass, trigger: "blur" }],
        name: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        code: [{ required: true, message: "请输入验证码", trigger: "blur" }],
        companyName: [
          { required: true, message: "请输入公司名称", trigger: "blur" },
        ],
        companyCode: [
          { required: true, message: "请输入公司编号", trigger: "blur" },
        ],
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            pattern: /^((0\d{2,3}-\d{7,8})|(1[12345789]\d{9}))$/,
            message: "手机号码不合法",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm(ref) {
      this.$refs[ref].validate((valid) => {
        if (valid) {
          tenantRegister(this.tenantDto).then((res) => {
            if (res.code == 200) {
              this.$message("注册成功");
              this.$router.push({ path: "/login" });
            } else if (res.code == 201) {
              this.$message("验证码已过期！");
            } else if (res.code == 501) {
              this.$message("验证码错误！");
            } else {
              this.$message("注册失败！");
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(ref) {
      this.$refs[ref].resetFields();
    },
    sendCode() {
      let TIME_COUNT = 60;
      this.count = TIME_COUNT;
      this.show = false;
      this.timer = setInterval(() => {
        if (this.count > 0 && this.count <= TIME_COUNT) {
          this.count--;
        } else {
          this.show = true;
          clearInterval(this.timer);
          this.timer = null;
        }
      }, 1000);
      sendCode(this.tenantDto.tenant.phone).then((res) => {
        if (res.code == 200) {
          this.$message({
            message: res.ok ? "验证码发送成功" : "验证码发送失败",
            type: res.code == 200 ? "success" : "error",
          });
        } else if (res.code == 201) {
          this.$message({
            message: res.ok ? "验证码已发送" : "验证码发送失败",
            type: res.code == 200 ? "success" : "error",
          });
        } else {
          this.$message("验证码发送失败");
        }
      });
    },
  },
  created() {},
};
</script>
<style>
.content {
  background-color: white;
  width: 500px;
  height: 620px;
  position: absolute;
  margin: auto;
  top: 10px;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 22px 30px 20px 20px;
  box-shadow: 0 0 10px 5px blue inset;
}
</style>
